import {
	BaseURL
} from '@/config/index.js'
import {
		useUserStoreWithOut
	} from '@/stores/modules/user.js'
	
// const userStore = useUserStoreWithOut()
export const get = (url, params, options) => {
	if (!isURL(url)) {
		url = BaseURL + url
	}
	// 头部添加Authorization
	options = addAuthorization(options)

	return uni.request({
		method: 'GET',
		url,
		data: params,
		...options
	}).then(res => {
		if (res.statusCode === 200) {
			return res.data
		} else {
			return Promise.reject(res)
		}
	}).then(res => {
		if (res.code === 200) {
			return res.data
		} else {
			if (!(options && options.hideErrorToast)) {
				showError(res.message)
			}
			responseErrorHandle(res)
			return Promise.reject(res)
		}
	})
}

export const post = (url, data, options) => {
	if (!isURL(url)) {
		url = BaseURL + url
	}
	// 头部添加Authorization
	options = addAuthorization(options)
	return uni.request({
		method: 'POST',
		url,
		data,
		...options
	}).then(res => {
		if (res.statusCode === 200) {
			return res.data
		} else {
			responseErrorHandle(res)
			return Promise.reject(res)
		}
	}).then(res => {
		if (res.code === 200) {
			return res.data
		} else {
			if (!(options && options.hideErrorToast)) {
				showError(res.message)
			}
			responseErrorHandle(res)
			return Promise.reject(res)
		}
	})
}

export const put = (url, data, options) => {
	if (!isURL(url)) {
		url = BaseURL + url
	}
	// 头部添加Authorization
	options = addAuthorization(options)
	return uni.request({
		method: 'PUT',
		url,
		data,
		...options
	}).then(res => {
		if (res.statusCode === 200) {
			return res.data
		} else {
			return Promise.reject(res)
		}
	}).then(res => {
		if (res.code === 200) {
			return res.data
		} else {
			if (!(options && options.hideErrorToast)) {
				showError(res.message)
			}
			responseErrorHandle(res)
			return Promise.reject(res)
		}
	})
}
export const deleteFn = (url, data, options) => {
	if (!isURL(url)) {
		url = BaseURL + url
	}
	// 头部添加Authorization
	options = addAuthorization(options)
	return uni.request({
		method: 'DELETE',
		url,
		data,
		...options
	}).then(res => {
		if (res.statusCode === 200) {
			return res.data
		} else {
			return Promise.reject(res)
		}
	}).then(res => {
		if (res.code === 200) {
			return res.data
		} else {
			if (!(options && options.hideErrorToast)) {
				showError(res.message)
			}
			responseErrorHandle(res)
			return Promise.reject(res)
		}
	})
}
export const upload = (url, options) => {
	if (!isURL(url)) {
		url = BaseURL + url
	}
	// options = addAuthorization(options)
	return uni.uploadFile({
		url,
		...options
	}).then(res => {
		if (res.statusCode === 200) {
			return res.data && JSON.parse(res.data)
		} else {
			responseErrorHandle(res)
			return Promise.reject(res)
		}
	})
}
export const uploadProgress = (url, options,progressUpdate) => {
	if (!isURL(url)) {
		url = BaseURL + url
	}
	const uploadTask = uni.uploadFile({
		url,
		...options
	})
	if(progressUpdate){
		uploadTask.onProgressUpdate(progressUpdate);
	}
	return uploadTask
}
const addAuthorization = (options) => {
	// 头部添加Authorization
	const token = uni.getStorageSync('token')
	if (token) {
		if (options) {
			if (options.header) {
				if (!options.header.Authorization) {
					options.header.Authorization = token
				}
			} else {
				options.header = {
					Authorization: token
				}
			}
		} else if (!options) {
			options = {
				header: {
					Authorization: token
				}
			}
		}
		return options
	}
}

const isURL = (url) => {
	return url && (url.startsWith('https://') || url.startsWith('http://'))
}

const showError = (msg) => {
	msg && uni.showToast({
		icon: 'none',
		title: msg
	})
}

const responseErrorHandle = (res) => {
	if (res.code === 10001) {
		uni.setStorageSync('token', null)
		uni.setStorageSync('userInfo', null)
		useUserStoreWithOut().$state.userInfo = null
		uni.reLaunch({
			url: '/pages/login/login'
		})
	}
}